package com.ruoyi.baseMain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.baseMain.domain.BaseCustom;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.dto.RoleDTO;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.workflow.domain.WfDeployForm;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import com.ruoyi.baseMain.domain.bo.BaseAccountBo;
import com.ruoyi.baseMain.domain.vo.BaseAccountVo;
import com.ruoyi.baseMain.domain.BaseAccount;
import com.ruoyi.baseMain.mapper.BaseAccountMapper;
import com.ruoyi.baseMain.service.IBaseAccountService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * 开户总表Service业务层处理
 *
 * @author linziheng
 * @date 2023-12-31
 */
@RequiredArgsConstructor
@Service
public class BaseAccountServiceImpl implements IBaseAccountService {

    private final BaseAccountMapper baseMapper;
    private final SysUserMapper sysUserMapper;
    private final SysDeptMapper sysDeptMapper;
    @Override
    public R<List<Map>> querySearch(String customName){
        return R.ok(baseMapper.querySearch(customName));
    }

    /**
     * 查询开户总表
     */
    @Override
    public BaseAccountVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public  BaseAccountVo queryByAccountId(String accountId){
        return baseMapper.selectVoOne(new LambdaQueryWrapper<BaseAccount>()
            .eq(BaseAccount::getAccountId, accountId));
    }

    /**
     * 查询开户总表列表
     */
    @Override
    public TableDataInfo<BaseAccountVo> queryPageList(BaseAccountBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        String user = "";
        for (RoleDTO role : loginUser.getRoles()) {
            if(1719534201567039490L == role.getRoleId() || 1L== role.getRoleId() || 1720783927643512833L == role.getRoleId()
                || 1720783977803194369L == role.getRoleId()|| 1741717746987237377L == role.getRoleId()|| 1750333840374910977L == role.getRoleId()){
                user = "admin";
                break;
            }
        }
        if(!"admin".equals(user)){
            bo.setSaleUserName(loginUser.getNickName());
        }
        SysUser uuu = sysUserMapper.selectUserById(loginUser.getUserId());
        SysDept sysDept = new SysDept();
        if(!"admin".equals(user)){
            sysDept = sysDeptMapper.findDeptByLeader(uuu.getRemark());
            if(!Objects.isNull(sysDept)){
                //根据id 查询部门名称
                bo.setBusinessUnitName(sysDept.getDeptName());
                bo.setSaleUserName(null);
            }else{
                bo.setSaleUserName(uuu.getNickName());
            }

            //业务中泰看所有
            if(uuu.getDeptId()==1811377323661430787L){
                bo.setBusinessUnitName(null);
                bo.setSaleUserName(null);
            }
        }

        LambdaQueryWrapper<BaseAccount> lqw = buildQueryWrapper(bo);
        Page<BaseAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询开户总表列表
     */
    @Override
    public List<BaseAccountVo> queryList(BaseAccountBo bo) {
        LambdaQueryWrapper<BaseAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseAccount> buildQueryWrapper(BaseAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseAccount> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCreateDate() != null, BaseAccount::getCreateDate, bo.getCreateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), BaseAccount::getAccountId, bo.getAccountId());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomName()), BaseAccount::getCustomName, bo.getCustomName());
        lqw.eq(StringUtils.isNotBlank(bo.getBidding()), BaseAccount::getBidding, bo.getBidding());
        lqw.like(StringUtils.isNotBlank(bo.getAccountName()), BaseAccount::getAccountName, bo.getAccountName());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), BaseAccount::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getIndustryType()), BaseAccount::getIndustryType, bo.getIndustryType());
        lqw.eq(StringUtils.isNotBlank(bo.getAparty()), BaseAccount::getAparty, bo.getAparty());
        lqw.eq(StringUtils.isNotBlank(bo.getShopId()), BaseAccount::getShopId, bo.getShopId());
        lqw.eq(StringUtils.isNotBlank(bo.getSignatory()), BaseAccount::getSignatory, bo.getSignatory());
        lqw.like(StringUtils.isNotBlank(bo.getShopName()), BaseAccount::getShopName, bo.getShopName());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountBelong()), BaseAccount::getAccountBelong, bo.getAccountBelong());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessUnitName()), BaseAccount::getBusinessUnitName, bo.getBusinessUnitName());

        lqw.eq(StringUtils.isNotBlank(bo.getSaleUserName()), BaseAccount::getSaleUserName, bo.getSaleUserName());
        if(Strings.isNotEmpty(bo.getIsNullList())){
            switch (bo.getIsNullList()) {
                case "0":
                    // 客户简称为空
                    lqw.isNull(BaseAccount::getCustomName);
                    break;
                case "1":
                    //业务部门为空
                    lqw.isNull(BaseAccount::getBusinessUnitName);
                    break;
                case "2":
                    //均为空
                    lqw.isNull(BaseAccount::getCustomName);
                    lqw.isNull(BaseAccount::getBusinessUnitName);
                    break;
                case "3":
                    //均不为空
                    lqw.isNotNull(BaseAccount::getCustomName);
                    lqw.isNotNull(BaseAccount::getBusinessUnitName);
                    break;
                default:
            }
        }
        lqw.orderByDesc(BaseAccount::getCreateDate);
        return lqw;
    }

    /**
     * 新增开户总表
     */
    @Override
    public Boolean insertByBo(BaseAccountBo bo) {
        BaseAccount add = BeanUtil.toBean(bo, BaseAccount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改开户总表
     */
    @Override
    public Boolean updateByBo(BaseAccountBo bo) {
        BaseAccount update = BeanUtil.toBean(bo, BaseAccount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量修改开户总表
     */
    @Override
    public Boolean batchEdit(BaseAccountBo bo) {
        BaseAccount update = BeanUtil.toBean(bo, BaseAccount.class);
        return baseMapper.batchEdit(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseAccount entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除开户总表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int getWeekList(String date) {
        return baseMapper.getWeekList(date);
    }

    @Override
    public int countAccount() {
        return baseMapper.countAccount();
    }
}
